package com.dense.kuiniu.floatcabinet.mvp.manager.bord;

import android.util.SparseArray;

import com.kuiniu.serialport.manager.SerialControlManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import timber.log.Timber;

/**
 * @author Administrator
 * @date 2020/11/17
 */
public class MessageCenter implements IAction {
    private IMessage mMessage;

    public MessageCenter(IMessage message, MessageObserver observer) {
        mMessage = message;
        observer.setMessageCenter(this);
    }

    public void doorControlOpenMessage(int deviceNumber, int doorNumber, boolean isOpen) {


    }

    public void doorStatusChange(int deviceId, byte[] status) {
        Timber.e("门状态变更，板号=%s", deviceId);
        DoorManager.getInstance().updateDoorState(deviceId, status);
        List<Door> change2OpenList = DoorManager.getInstance().getChange2OpenDoorList();
        List<Door> change2CloseList = DoorManager.getInstance().getChange2CloseDoorList();
        if (mMessage != null) {
            mMessage.doorChange(change2OpenList, change2CloseList);
        }
    }

    public void doorStatusInit(int deviceId, byte[] status) {
        Timber.e("门状态初始化完成，板号=%s", deviceId);
        mMessage.initDoorState(deviceId, status);
    }

    public void inventory(Map<Integer, Map<String, Integer>> mapMap) {
        Timber.e("盘点完成");
        for (Map.Entry<Integer, Map<String, Integer>> entry : mapMap.entrySet()) {
            int doorNumber = entry.getKey();
            Door door = DoorManager.getInstance().getDoor(doorNumber);
            if (door != null) {
                List<String> oldEpcList = door.getEpc();
                //减少的数据
                List<String> reduceList = new ArrayList<>();
                //最新的数据
                Map<String, Integer> scanMap = entry.getValue();
                //增加的数据
                Map<String, Integer> cloneMap = new HashMap<>(scanMap);
                if (oldEpcList != null) {
                    for (String epc : oldEpcList) {
                        if (scanMap.containsKey(epc)) {
                            cloneMap.remove(epc);
                        } else {
                            reduceList.add(epc);
                        }
                    }
                }
                if (!reduceList.isEmpty() || cloneMap.size() > 0) {
                    List<String> newList = new ArrayList<>(scanMap.keySet());
                    door.setEpc(newList);
                    //表示最新的数据和原数据不同
                    if (mMessage != null) {
                        //盘点后更改的数据
                        mMessage.changeList(doorNumber, reduceList, new ArrayList<>(cloneMap.keySet()),
                                newList);
                    }

                }
            }
        }
    }

    @Override
    public void open(int doorNumber) {
        Timber.e("调用开门操作，门号=%s", doorNumber);
        int[] d = DoorNumberProvider.Door2DeviceAndDoor(doorNumber);
        SerialControlManager.getInstance().openOrClose(d[0], d[1], true);
    }

    @Override
    public void inventorySingle(int doorNumber) {
        Timber.e("调用单个门盘点，门号=%s", doorNumber);
        int[] d = DoorNumberProvider.Door2DeviceAndDoor(doorNumber);
        SerialControlManager.getInstance().readRFID("2", d[0], d[1]);
    }

    @Override
    public void inventoryAll() {

        SparseArray<int[]> doorNumbers = DoorManager.getInstance().getNumbers();
        for (int i = 0; i < doorNumbers.size(); i++) {
            int deviceId = doorNumbers.keyAt(i);
            Timber.e("调用全部盘点，版号=%s", deviceId);
            int[] doors = doorNumbers.get(deviceId);
            SerialControlManager.getInstance().readRFID("5", deviceId, 2);
        }
    }

    @Override
    public void initDoorStatus(int deviceId) {
        Timber.e("板号=" + deviceId + "初始化============================");
        SerialControlManager.getInstance().initStatus(deviceId);
    }
}